﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_GoodSales_Stat2 : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReadAction();
            BindData();
        }
    }

    #region 窗体设计器生成的代码

    protected override void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestLoginPage = "BalanceCenter/GoodSales_Stat2.aspx";

        base.OnInit(e);
    }

    #endregion

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void g_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(DrawGridFooterForG));
        }
    }

    protected void DrawGridFooterForG(HtmlTextWriter output, Control ctl)
    {
        int Amount = 0;             //总销售数量
        double SumPrice = 0;        //销售总额

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //Amount += ShoveEL._Convert.StrToInt(dt.Rows[i]["Amount"].ToString(), 0);
                //SumPrice += ShoveEL._Convert.StrToDouble(dt.Rows[i]["SumPrice"].ToString(), 0);
            }

            output.Write("<td align='center'>总计</td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'>" + Amount + "</td>");
            output.Write("<td align='center'>" + SumPrice + "</td>");
            output.Write("<td align='center'></td>");
        }
    }

    private void ReadAction()
    {
        this.hGoodID.Value = ShoveEL._Web.Utility.GetRequest("GoodID");
        this.hStartTime.Value = ShoveEL._Web.Utility.GetRequest("StartTime");
        this.hEndTime.Value = ShoveEL._Web.Utility.GetRequest("EndTime");
    }

    private void BindData()
    {
        string Condition = "1=1";

        if (hGoodID.Value != "")
        {
            Condition += " and GoodID = '" + hGoodID.Value.Trim() + "'";
        }
        if ((hStartTime.Value != "") && (hEndTime.Value != ""))
        {
            Condition += " and SalesDate between '" + hStartTime.Value.Trim() + "' and dateadd(dd,1,'" + hEndTime.Value.Trim() + "')";
        }

        try
        {
            dt = DAL.Views.V_GoodSales.Select("", Condition, "");

            if (dt != null && dt.Rows.Count > 0)
            {
                g.DataSource = dt;
                g.DataBind();
                gPager.Visible = (dt.Rows.Count > 0);
            }
        }
        catch { }
    }

}
